package com.zxf.course.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zxf.course.bean.Classes;
import com.zxf.course.bean.Student;
import com.zxf.course.service.ClassesService;
import com.zxf.course.service.StudentService;

@Controller
@RequestMapping("/student")//提取公共部分
public class StudentController{
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClassesService classesService;
	
	@RequestMapping(value="/findStudent")
	public String selectById(Model model,Integer id) {
		
		Student selectStudentById = studentService.selectStudentById(id);
		
		model.addAttribute("student",selectStudentById);
		
		return "student";
	}
	
	@RequestMapping(value="/login")
	public String login() {
		
		return "login";
	}
	
	@RequestMapping(value="/index")
	public String index() {
		
		return "index";
	}
	
	@RequestMapping(value="/register")
	public String register() {
		
		return "register";
	}
	
	@RequestMapping(value="/404")
	public String unResponse() {
		return "404";
	}
	
	@ResponseBody
	@RequestMapping(value="/studentLogin")
	public String studentLogin(Model model,Student student,HttpSession session) {
		
		boolean studentStatus = studentService.studentLogin(student);
		if(studentStatus) {
			session.setAttribute("student", student);
		}
		return studentStatus ? "success" : "failed";
		
       /* Subject subject = SecurityUtils.getSubject();
        
        UsernamePasswordToken token = new UsernamePasswordToken(student.getStudentName(), student.getStudentContact());
        
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            model.addAttribute("userName", "用户名错误！");
            return "login";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            model.addAttribute("passwd", "密码错误");
            return "login";
        }*/
	}
	
	@RequestMapping(value="/studentList")
	public String studentList(Model model) {
		
		List<Student> studentList = studentService.selectStudentList();
		
		model.addAttribute("studentList", studentList);
		
		return "student/student-list";
	}
	@ResponseBody
	@RequestMapping(value="/deleteStudent")
	public String deleteStudent(Integer studentNo,HttpServletRequest request) {
		
	    Boolean deleteStatus = studentService.deleteStudent(studentNo);
	    
		return deleteStatus ? "success" : "failed";
	}
	
	@RequestMapping(value="/uploadFile")
	public String studentImgUpload(MultipartFile uploadFile,Student student) throws IllegalStateException, IOException {
        
		String studentImg = new Date().getTime()+"";
		String path="E:/upload/"+studentImg.trim()+uploadFile.getOriginalFilename();
         
        File newFile=new File(path);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        uploadFile.transferTo(newFile);
        System.out.println(path);
        
        student.setStudentImg(path);
        studentService.addStudent(student);
        
        return "redirect:/student/studentList"; 
		
	}
	
	@RequestMapping("/registerStudent")
	public String registerStudent(Student student) {
		
		studentService.addStudent(student);
		
		return "login";
	}
	
	@RequestMapping(value="/addStudentFirst")
	public String addStudent1(Model model) {
		
		List<Classes> classesList = classesService.selectAllClasses();
		
		model.addAttribute("classesList", classesList);
		
		return "student/student-add";
	}
}
